package com.tech.algorithm.sort;

import com.sun.deploy.panel.ITreeNode;
import sun.awt.image.IntegerInterleavedRaster;

import java.util.Arrays;

/**
 * @Description:冒泡排序
 * @Author: zl
 * @Date: 2019/5/31 9:08
 */
public class BubbleSort {

    static  void BubbleSort(int arr[], int len)
    {
        int i = 0;
        int tmp = 0;
        for (i = 0; i < len - 1; i++)//确定排序趟数
        {
            int j = 0;
            int flag = 0;
            for (j = 0; j < len - 1 - i; j++)//确定比较次数
            {
                if (arr[j]>arr[j + 1])
                {
                    //交换
                    tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    flag = 1;//加入标记
                }
            }
            if (flag == 0)//如果没有交换过元素，则已经有序
            {
                System.out.println("return");
                System.out.println(Arrays.toString(arr));
                //return;
            }
            System.out.println("i="+i);
        }

    }



    public static void main(String[] args) {

        long t1 = System.currentTimeMillis();
        int [] ints = new int[800000];
        for (int i=0;i<ints.length;i++) {
            ints[i] = (int)(Math.random()*800000);
        }
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);

        int arr[] = new int[]{5, 4, 3, -2, 1};
        BubbleSort(arr,5);
//        int temp;
//        for (int i = arr.length - 1; i > 0; i--) {
//            for (int j = 0; j < i; j++) {
//                if (arr[j] > arr[j + 1]) {
//                    temp = arr[j + 1];
//                    arr[j + 1] = arr[j];
//                    arr[j] = temp;
//                }
//            }
//        }
//        System.out.println(Arrays.toString(arr));
    }
}











